<?php

/*

	Author:	Cedric McMillan Junior

	Date:	March 15th 2012
	
	Aim:	general purpose utility code

*/



require_once('pqp/classes/PhpQuickProfiler.php');
class PQPExample {
	
	private $profiler;
	private $db = '';
	
	public function __construct() {
		$this->profiler = new PhpQuickProfiler(PhpQuickProfiler::getMicroTime());
	}
	
	public function init() 
	{
		global	$gQueries;
		global	$gQueriesArray;
		$user					=	MYSQL_USER;
		$pass					=	MYSQL_PASS;
		$host					=	MYSQL_HOST;
		$db						=	MYSQL_DBASE;
		$queries				=	0;
		$gQueriesArray			=	array();

        $this->db		=	mysql_connect($host, $user, $pass);
		mysql_select_db($db, $this->db);
	}
	
	
	
	/*-------------------------------------------
	     EXAMPLES OF THE 4 CONSOLE FUNCTIONS
	-------------------------------------------*/
	
	public function sampleConsoleData() {
		try {
			Console::log('Begin logging data');
			Console::logMemory($this, 'PQP Example Class : Line '.__LINE__);
			Console::logSpeed('Time taken to get to line '.__LINE__);
			Console::log(array('Name' => 'Ryan', 'Last' => 'Campbell'));
			Console::logSpeed('Time taken to get to line '.__LINE__);
			Console::logMemory($this, 'PQP Example Class : Line '.__LINE__);
			Console::log('Ending log below with a sample error.');
			throw new Exception('Unable to write to log!');
		}
		catch(Exception $e) {
			Console::logError($e, 'Sample error logging.');
		}
	}
	
	/*-----------------------------------
	     EXAMPLE MEMORY LEAK DETECTED
	------------------------------------*/
	
	public function sampleMemoryLeak() {
		$ret = '';
		$longString = 'This is a really long string that when appended with the . symbol 
					  will cause memory to be duplicated in order to create the new string.';
		for($i = 0; $i < 10; $i++) {
			$ret = $ret . $longString;
			Console::logMemory($ret, 'Watch memory leak -- iteration '.$i);
		}
	}
	

	# my logging of an item
	public function logSpeed($text,$line=__LINE__) {
		Console::logSpeed("$text line:$line");
	}

	# my logging of an memory 
	public function logMemory($line=__LINE__,$variable="",$name="") 
	{

		Console::logMemory($variable, "$name : Line:$line");
	}

	
	public function __destruct() {
#		$this->profiler->display($this->db);
	}


	public function display() {
		$this->profiler->display($this->db);
	}
	
}


# handles init for pqp debugger
if	(PQP_DEBUG == true)
{
	$pqp = new PQPExample();
	$pqp->init();
}
